/**
 * 基础配置信息
 */
var setter = {
    menuUrl: "api/menu.json",    // 菜单接口
    loginHref: 'page/login.html',
    settingHref: 'page/system/setting.html',
    urlHashLocation: false,      // 是否打开hash定位
    bgColorDefault: 'blue',      // 主题默认配置
    multiModule: false,          // 是否开启多模块
    pageAnim: true,             // iframe窗口动画
    containerCls: '.layui-layout-admin',
    logoInfo: {
        title: "M-ADMIN",
        image: "images/logo.png",
        href: ""
    },
    homeInfo: {
        title: "首页",
        i18n: "home",
        icon: "fa fa-home",
        href: "page/home.html"
    },
    store: {
        tableName: 'layuimulti',
        homeHref: 'homeHref',
        tabInfo: 'tabInfo',
        bgColorId: 'bgColorId',
        langId: 'lang',
        sideShrink: 'sideShrink',
        headerHidden: 'headerHidden',
        footerHidden: 'footerHidden',
        token: 'token'
    }
}
/**
 * 可以当做通用配置类，不需要引入layui框架
 */
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(decodeURIComponent(r[2]));
    return null;
}
function getStoreDataJson(storage = sessionStorage) {
    var store = storage.getItem(setter.store.tableName)
    try {
        store = JSON.parse(store)
        return store || {}
    } catch(e) {}
    return {}
}
function getSessionData(key) {
    return getStoreDataJson()[key]
}
function setSessionData(key, value) {
    var store = getStoreDataJson()
    try {
        store[key] = value;
        sessionStorage.setItem(setter.store.tableName, JSON.stringify(store));
    } catch(e) {}
}
function getData(key) {
    return getStoreDataJson(localStorage)[key]
}
function setData(key, value) {
    var store = getStoreDataJson(localStorage)
    try {
        store[key] = value;
        localStorage.setItem(setter.store.tableName, JSON.stringify(store))
    } catch(e) {}
}
function getToken() {
    return getSessionData()[setter.store.token] || getQueryString('token')
}
function getTheme() {
    return getData(setter.store.bgColorId) || setter.bgColorDefault
}
function getLang() {
    return getData(setter.store.langId) || navigator.language || navigator.userLanguage
}
/**
 * 打开tab页，用发消息方式，支持跨域
 * @param {*} url 地址
 * @param {*} title 标题
 */
function openTab(url, title, target = '_self') {
    top.postMessage({type: 'openTab', data: {url: url, title: title, target: target}})
}
/**
 * 关闭tab，用发消息方式，支持跨域
 * @param type: self, other, all
 */
function closeTab(type = 'self') {
    var method = type;
    switch(type) {
        case 'self':
            method = 'closeCurrentTab'
            break;
        case 'other':
            method = 'closeOtherTab'
            break;
        case 'all':
            method = 'closeAllTab'
            break;
        default:
            break;
    }
    top.postMessage({type: 'closeTab', data: method})
}
/**
 * 页面接收消息
 * @param {*} callback 
 */
function receiveMessage(callback) {
    window._mqueueMessage = window._mqueueMessage || []
    if (callback && typeof callback === "function") {
        window._mqueueMessage.push(callback)
        if (window._mqueueMessage.length < 2) {
            var _dealWithMessage = function(event) {
                Object.values(window._mqueueMessage).map(function(v) {
                    typeof v === 'function' && v(event)
                })
            }
            window.removeEventListener('message', _dealWithMessage);
            window.addEventListener('message', _dealWithMessage);
        }
    }
}